﻿using System;
using System.Collections.Generic;
using System.Text;
using SIOsystem.Model.ClassifyDataSetTableAdapters;
using System.Data;
using System.Windows.Forms;
using SIOsystem.Model.OperatorDataSetTableAdapters;

namespace SIOsystem.Control
{
    class DictionaryClass
    {
        static private DataTable getClass(long CID,string defaultString)
        {
            try
            {
                using (ClassifyTableAdapter db = new ClassifyTableAdapter())
                {
                    DataTable dt = db.GetClassifyByPID(CID);
                    if (!defaultString.Equals(""))
                    {
                        DataRow dr = dt.NewRow();
                        dr["CID"] = -1;
                        dr["CName"] = defaultString;
                        dt.Rows.InsertAt(dr, 0);
                    }
                    return dt;
                }
            }
            catch (Exception ex)
            {
                throw new Exception("获取分类出错", ex);
            }
        }

        static private DataTable getDepartment(string defaultString)
        {
            try
            {
                using (DepartmentTableAdapter db = new DepartmentTableAdapter())
                {
                    DataTable dt = db.GetDepartment();
                    DataRow dr = dt.NewRow();
                    if (!defaultString.Equals(""))
                    {
                        dr["DID"] = -1;
                        dr["DeptName"] = defaultString;
                        dt.Rows.InsertAt(dr, 0);
                    }
                    return dt;
                }
            }
            catch (Exception ex)
            {
                throw new Exception("获取部门出错", ex);
            }
        }

        static public bool fillMainClass(ref ComboBox mainList)//复用fillModelClass
        {
            fillModelClass(ref mainList, 0);
            return true;
        }

        static public bool fillProductClass(ref ListBox productList, long MID)
        {
            if (productList.Items.Count > 0)
            {
                productList.DataSource = null;
                productList.Items.Clear();
            }
            productList.DisplayMember = "CName";
            productList.ValueMember = "CID";
            productList.DataSource = getClass(MID, "");
            return true;
        }

        static public bool fillProductClass(ref CheckedListBox productList, long MID)
        {
            if (productList.Items.Count > 0)
            {
                productList.DataSource = null;
                productList.Items.Clear();
            }
            productList.ValueMember = "CID";
            productList.DataSource = getClass(MID, "");
            productList.DisplayMember = "CName";
            return true;
        }

        static public bool fillModelClass(ref ComboBox modelList, long PID)
        {
            if (modelList.Items.Count > 0)
            {
                modelList.DataSource = null;
                modelList.Items.Clear();
            }
            modelList.DisplayMember = "CName";
            modelList.ValueMember = "CID";
            modelList.DataSource = getClass(PID,"请选择...");
            if (modelList.Items.Count == 2)
            {
                modelList.SelectedIndex = 1;
            }
            return true;
        }

        static public bool fillModelClass(ref ComboBox modelList, long PID,string defaultString)
        {
            if (modelList.Items.Count > 0)
            {
                modelList.DataSource = null;
                modelList.Items.Clear();
            }
            modelList.DisplayMember = "CName";
            modelList.ValueMember = "CID";
            modelList.DataSource = getClass(PID, defaultString);
            if (modelList.Items.Count > 0)
            {
                modelList.SelectedIndex = 0;
            }
            return true;
        }

        static public bool fillDepartment(ref ComboBox departmentList)
        {
            if (departmentList.Items.Count > 0)
            {
                departmentList.DataSource = null;
                departmentList.Items.Clear();
            }
            departmentList.DisplayMember = "DeptName";
            departmentList.ValueMember = "DID";
            departmentList.DataSource = getDepartment("请选择...");
            if (departmentList.Items.Count == 2)
            {
                departmentList.SelectedIndex = 1;
            }
            return true;
        }

        static public bool fillDepartment(ref ListBox departmentList)
        {
            if (departmentList.Items.Count > 0)
            {
                departmentList.DataSource = null;
                departmentList.Items.Clear();
            }
            departmentList.DisplayMember = "DeptName";
            departmentList.ValueMember = "DID";
            departmentList.DataSource = getDepartment("");
            return true;
        }

        static public bool fillSetMainClass(ref ComboBox mainList)
        {
            if (mainList.Items.Count > 0)
            {
                mainList.DataSource = null;
                mainList.Items.Clear();
            }
            mainList.DisplayMember = "CName";
            mainList.ValueMember = "CID";
            mainList.DataSource = getClass(0, "未分类产品");
            return true;
        }

        static public bool fillOperator(ref DataGridView operatorList)
        {
            try
            {
                using (OperatorTableAdapter db = new OperatorTableAdapter())
                {
                    operatorList.DataSource = db.GetOperator();
                    return true;
                }
            }
            catch (Exception ex)
            {
                throw new Exception("获取操作员出错", ex);
            }
        } 

    }
}
